package cn.gyouzhe.service.impl;

import cn.gyouzhe.service.ICacheService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 转链缓存
 *
 * @author wangchuan
 * @datetime 2018/4/13 23:21
 */
@Service
public class CacheService implements ICacheService {
    private Cache<String, Object> cache = CacheBuilder.newBuilder()
            .maximumSize(5000)
            .expireAfterAccess(1, TimeUnit.HOURS)
            .build();

    @Override
    public void addCache(String key, Object val) {
        cache.put(key, val);
    }

    @Override
    public void removeCache(String key) {
        cache.invalidate(key);
    }

    @Override
    public Object getCache(String key) {
        return cache.getIfPresent(key);
    }
}
